package com.heima.wemedia.builder;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import org.springframework.beans.BeanUtils;

import java.util.Date;

public class ArticleDtoBuilder {
    private ArticleDto dto = new ArticleDto();
    private WmNews wmNews;
    private WmChannelMapper wmChannelMapper;
    private WmUserMapper wmUserMapper;
    public ArticleDtoBuilder(WmNews wmNews,WmChannelMapper wmChannelMapper,WmUserMapper wmUserMapper){
          this.wmNews=wmNews;
          this.wmUserMapper=wmUserMapper;
          this.wmChannelMapper=wmChannelMapper;
    }

   public ArticleDtoBuilder buildBeanCopy(){
       BeanUtils.copyProperties(wmNews,dto);
       return this;
   }
    public ArticleDtoBuilder buildBasic(){
        dto.setLayout(wmNews.getType());
        dto.setCreatedTime(new Date());
        dto.setViews(0);
        dto.setCollection(0);
        dto.setComment(0);
        dto.setViews(0);
        dto.setLikes(0);
        if (null!=wmNews.getArticleId()){
            dto.setId(wmNews.getArticleId());
        }
        return this;
    }
    public ArticleDtoBuilder buildAuthor(){
        dto.setAuthorId(wmNews.getUserId().longValue());
        if (null!=wmNews.getUserId()){
            WmUser wmUser = wmUserMapper.selectOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getId, wmNews.getUserId()));
            if (null!=wmUser){
                dto.setAuthorName(wmUser.getName());
            }
        }
        return this;
    }
    public ArticleDtoBuilder buildChannel(){
        if (null!=wmNews.getChannelId()){
            WmChannel wmChannel = wmChannelMapper.selectOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, wmNews.getChannelId()));
            dto.setChannelName(wmChannel.getName());
        }
        return this;
    }
    public ArticleDto bulid(){
        return dto;
    }


}
